package com.ykht.proj.comsrv.mq;

import cn.hutool.json.JSONUtil;
import com.ykht.proj.comsrv.constant.ComConstant;
import com.ykht.proj.comsrv.service.VSubscribeMsgService;
import com.ykht.proj.comsrv.vo.SubscribeMsgActivityNotice;
import com.ykht.proj.comsrv.vo.SubscribeMsgCardExpire;
import com.ykht.proj.comsrv.vo.SubscribeMsgCouponExpire;
import com.ykht.proj.comsrv.vo.SubscribeMsgCouponTouTiaoExpire;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class SubscribeMsgComsumer {

	//topic：和消费者发送的topic相同
	//group：不用和生产者group相同


	@Slf4j
	@Component
	@RocketMQMessageListener(topic = ComConstant.SUBSCRIBE_MSG_TOPIC, consumerGroup = ComConstant.SUBSCRIBE_MSG_COUPON_EXPIRE_GROUP, selectorExpression = ComConstant.SUBSCRIBE_MSG_COUPON_EXPIRE)
	public static class CouponExprieComsumer implements RocketMQListener<SubscribeMsgCouponExpire>{
		@Resource
		private VSubscribeMsgService vSubscribeMsgService;

		@Override
		public void onMessage(SubscribeMsgCouponExpire msg) {
			//故意不让抛出异常，不然会重新消费
			try {
				log.info("优惠券过期接收消息{}", JSONUtil.toJsonStr(msg));
				vSubscribeMsgService.sendSubscribeMsg(msg);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	@Slf4j
	@Component
	@RocketMQMessageListener(topic = ComConstant.SUBSCRIBE_MSG_TOPIC, consumerGroup = ComConstant.SUBSCRIBE_MSG_COUPON_EXPIRE_TOUTIAO_GROUP, selectorExpression = ComConstant.SUBSCRIBE_MSG_COUPON_EXPIRE_TOUTIAO)
	public static class CouponExprieTouTiaoComsumer implements RocketMQListener<SubscribeMsgCouponTouTiaoExpire>{
		@Resource
		private VSubscribeMsgService vSubscribeMsgService;

		@Override
		public void onMessage(SubscribeMsgCouponTouTiaoExpire msg) {
			//故意不让抛出异常，不然会重新消费
			try {
				log.info("头条优惠券过期接收消息{}", JSONUtil.toJsonStr(msg));
				vSubscribeMsgService.sendSubscribeMsg(msg);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	@Slf4j
	@Component
	@RocketMQMessageListener(topic = ComConstant.SUBSCRIBE_MSG_TOPIC, consumerGroup = ComConstant.SUBSCRIBE_MSG_CARD_EXPIRE_GROUP, selectorExpression = ComConstant.SUBSCRIBE_MSG_CARD_EXPIRE )
	public static class CardExprieComsumer implements RocketMQListener<SubscribeMsgCardExpire>{
		@Resource
		private VSubscribeMsgService vSubscribeMsgService;

		@SneakyThrows
		@Override
		public void onMessage(SubscribeMsgCardExpire msg) {
			try {
				log.info("卡过期接收消息{}", JSONUtil.toJsonStr(msg));
				vSubscribeMsgService.sendSubscribeMsg(msg);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
	@Slf4j
	@Component
	@RocketMQMessageListener(topic = ComConstant.SUBSCRIBE_MSG_TOPIC, consumerGroup = ComConstant.SUBSCRIBE_MSG_ACTIVITY_NOTICE_GROUP, selectorExpression = ComConstant.SUBSCRIBE_MSG_ACTIVITY_NOTICE)
	public static class ActivityNoticeComsumer implements RocketMQListener<SubscribeMsgActivityNotice>{
		@Resource
		private VSubscribeMsgService vSubscribeMsgService;
		@SneakyThrows
		@Override
		public void onMessage(SubscribeMsgActivityNotice msg) {
			try {
				log.info("活动接收消息{}", JSONUtil.toJsonStr(msg));
				vSubscribeMsgService.sendSubscribeMsg(msg);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
